

function say() {
    alert("LC:986. 区间列表的交集;待优化");
}
say();

/**
 * @param {number[][]} firstList
 * @param {number[][]} secondList
 * @return {number[][]}
 */
var intervalIntersection = function(firstList, secondList) {
    //12:37 - 55

    var res = []
    for(var i = 0; i < secondList.length; i ++){
        var secItem = secondList[i]
        for(var j = 0; j < firstList.length; j++){
            var firstItem = firstList[j]
            if(secItem[0] <= firstItem[1] && secItem[1] >= firstItem[0]){
                var input = [Math.max(firstItem[0],secItem[0]) ,Math.min(firstItem[1],secItem[1])]
                res.push(input)
            }
        }
    }

    return res

};


//----优化:参考 https://leetcode-cn.com/problems/interval-list-intersections/solution/jiu-pa-ni-bu-dong-shuang-zhi-zhen-by-hyj8/

var intervalIntersection = function(firstList, secondList) {
    //10:00-10:15

    var fIdx = 0
    var sIdx = 0
    var res = []

    while(fIdx < firstList.length && sIdx < secondList.length){
        var left = Math.max(firstList[fIdx][0], secondList[sIdx][0])
        var right = Math.min(firstList[fIdx][1], secondList[sIdx][1])
        if(left <= right){
            res.push([left, right])
        }

        if(firstList[fIdx][1] < secondList[sIdx][1]){
            fIdx++
        }else {
            sIdx++
        }
    }

    return res
};
